# 编写权限表的业务
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
# 导入Django权限类
from django.contrib.auth.models import Permission, ContentType

from apps.meiduo_admin.serializers.perms import PermissionSerialzier
from apps.meiduo_admin.utils import NumPage


class PermsView(ModelViewSet):
    # 指定序列化器
    serializer_class = PermissionSerialzier
    # 指定查询集
    queryset = Permission.objects.all()
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    # 获取权限类型数据
    def content_types(self, request):
        # 查询权限类型表
        contenttypes = ContentType.objects.all()
        # 构造返回结果
        data_list = []
        for content in contenttypes:
            data_list.append({
                'id': content.id,
                'name': content.name
            })

        return Response(data_list)
